class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        return self.valid(root, float("-inf"), float("inf"))

    def valid(self,root, left, right):
        if root is None:
            return True

        if root.val >= right or root.val <= left:
            return False

        return self.valid(root.left, left, root.val) and self.valid(root.right, root.val, right)